package com.java.zhuiyun.api.video_manage.service.impl;

import com.java.zhuiyun.api.file_manager.entity.FileManager;
import com.java.zhuiyun.api.sys_user.entity.SysUser;
import com.java.zhuiyun.api.sys_user.mapper.SysUserMapper;
import com.java.zhuiyun.api.video_manage.mapper.VideoManageMapper;
import com.java.zhuiyun.api.video_manage.service.VideoManageService;
//import com.java.zhuiyun.util.EmailUtils;
import com.java.zhuiyun.util.pageUtil.MybatisPageHelper;
import com.java.zhuiyun.util.pageUtil.PageRequest;
import com.java.zhuiyun.util.pageUtil.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;

/**
 * @ClassName VideoManageServiceImpl
 * @Description TODO
 * @Author MJL
 * @Date 2023/8/21 19:45
 **/
@Service
public class VideoManageServiceImpl implements VideoManageService {
    @Resource
    VideoManageMapper videoManageMapper;
    @Resource
    JavaMailSender javaMailSender;
//    @Resource
//    EmailUtils emailUtils;

    /**
    * @Author MJL
    * @Description todo 查询所有尚未审核文件
    * @Date 2023/8/21 19:53
    * @Return  * @param null
    */
    @Override
    public PageResult queryVideoManage(FileManager fileManager, PageRequest pageRequest) {
        MybatisPageHelper.startPage(pageRequest);
        PageResult pageResult = MybatisPageHelper.getPageResult(videoManageMapper.queryVideoManage(fileManager));
        return pageResult;
    }

    @Autowired
    SysUserMapper sysUserMapper;

    /**
    * @Author MJL
    * @Description todo 将尚未审核的文件进行审核通过
    * @Date 2023/8/22 10:52
    * @Return  * @param null
    */
    @Override
    public int auditSucceed(FileManager fileManager) {
        Example example = new Example(SysUser.class);
        example.createCriteria().andEqualTo("id",fileManager.getUserId());
        SysUser sysUser = sysUserMapper.selectOneByExample(example);
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        simpleMailMessage.setFrom("2193218465@qq.com");
        simpleMailMessage.setTo(sysUser.getEmail());
        simpleMailMessage.setText("【智学IT课堂】：您好！经审核，您在智学IT课堂上传的视频现已通过审核，请您尽快对视频进行分类。");
        simpleMailMessage.setSubject("智学IT课堂");
        System.out.println("开始发送");
        javaMailSender.send(simpleMailMessage);
        System.out.println("发送完成");
        return videoManageMapper.auditSucced(fileManager);
    }

    /**
    * @Author MJL
    * @Description todo 将尚未审核的视频进行审核失败
    * @Date 2023/8/22 11:25
    * @Return  * @param null
    */
    @Override
    public int auditFail(FileManager fileManager) {
        Example example = new Example(SysUser.class);
        example.createCriteria().andEqualTo("id",fileManager.getUserId());
        SysUser sysUser = sysUserMapper.selectOneByExample(example);
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        simpleMailMessage.setFrom("2193218465@qq.com");
        simpleMailMessage.setTo(sysUser.getEmail());
        simpleMailMessage.setText("【智学IT课堂】：您好！经审核，您在智学IT课堂上传的视频含有违规内容，审核未通过。请您检查视频内容重新上传。");
        simpleMailMessage.setSubject("智学IT课堂");
        System.out.println("开始发送");
        javaMailSender.send(simpleMailMessage);
        System.out.println("发送完成");
        return videoManageMapper.auditFail(fileManager);
    }

    @Override
    public Integer Preview(Integer fileManagerId) {
        return videoManageMapper.Preview(fileManagerId);
    }
}
